# coding: utf-8

from PySide6.QtCore import Qt, QSize
from PySide6.QtWidgets import QVBoxLayout, QListWidgetItem

from components import WaterfallLayout, TagListWidget
from server.api import LenovoAPI
from ..base import BaseInterface


class ClassicInterface(BaseInterface):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.classId = -1
        self.tagId = -1
        self.priceId = 0
        self.verticalLayout = QVBoxLayout(self.view)
        self.classListWidget = TagListWidget(self.view)
        self.tagListWidget = TagListWidget(self.view)
        self.priceListWidget = TagListWidget(self.view)
        self.waterfallLayout = WaterfallLayout()
        self.sumNum = 0
        self.__initLayout()
        self.connectSignals()

    def connectSignals(self):
        self.classListWidget.itemClicked.connect(self.onClassClicked)
        self.tagListWidget.itemClicked.connect(lambda: (self.clear(), self.loadWallpaperData()))
        self.priceListWidget.itemClicked.connect(lambda: (self.clear(), self.loadWallpaperData()))

    def initLoadData(self):
        self.load(LenovoAPI.classes, slot=self.onClassesResult)

    def __initLayout(self):
        self.setObjectName("ClassicInterface")
        self.classListWidget.setTag('分类：')
        self.tagListWidget.setTag('标签：')
        self.priceListWidget.setTag('资费：')

        self.verticalLayout.setContentsMargins(0, 0, 11, 0)
        self.verticalLayout.setAlignment(Qt.AlignmentFlag.AlignTop)
        self.verticalLayout.addWidget(self.classListWidget, 0, Qt.AlignmentFlag.AlignTop)
        self.verticalLayout.addWidget(self.tagListWidget, 0, Qt.AlignmentFlag.AlignTop)
        self.verticalLayout.addWidget(self.priceListWidget, 0, Qt.AlignmentFlag.AlignTop)
        self.verticalLayout.addLayout(self.waterfallLayout, 1)

    def onClassesResult(self, result: dict):
        data = result.get('data')
        for cls in data:
            cid = cls.get('classifyId')
            name = cls.get('classifyName')
            tags = cls.get('tags')
            priceList = cls.get('priceList')
            item = QListWidgetItem(name, self.classListWidget)
            item.setSizeHint(QSize(80, 40))
            item.setData(Qt.ItemDataRole.UserRole, cid)
            item.setData(Qt.ItemDataRole.UserRole + 1, tags)
            item.setData(Qt.ItemDataRole.UserRole + 2, priceList)
            item.setTextAlignment(Qt.AlignmentFlag.AlignCenter)
            item.setToolTip(name)
            self.classListWidget.addItem(item)
        self.classListWidget.setCurrentRow(0)
        self.onClassClicked(self.classListWidget.item(0))

    def onClassClicked(self, item: QListWidgetItem):
        tags = item.data(Qt.ItemDataRole.UserRole + 1)
        priceList = item.data(Qt.ItemDataRole.UserRole + 2)
        self.tagListWidget.clear()
        self.priceListWidget.clear()
        for tag in tags:
            tagId = tag.get('tagId')
            tagName = tag.get('tagName')
            item = QListWidgetItem(tagName, self.tagListWidget)
            item.setData(Qt.ItemDataRole.UserRole, tagId)
            item.setSizeHint(QSize(80, 40))
            item.setTextAlignment(Qt.AlignmentFlag.AlignCenter)
            item.setToolTip(tagName)
            self.tagListWidget.addItem(item)
        self.tagListWidget.setCurrentRow(0)
        for price in priceList:
            priceId = price.get('priceId')
            priceName = price.get('priceName')
            item = QListWidgetItem(priceName, self.priceListWidget)
            item.setData(Qt.ItemDataRole.UserRole, priceId)
            item.setSizeHint(QSize(80, 40))
            item.setTextAlignment(Qt.AlignmentFlag.AlignCenter)
            self.priceListWidget.addItem(item)
        self.priceListWidget.setCurrentRow(0)
        self.clear()
        self.loadWallpaperData()

    def loadWallpaperData(self):
        classId = self.classListWidget.currentItem().data(Qt.ItemDataRole.UserRole)
        tagId = self.tagListWidget.currentItem().data(Qt.ItemDataRole.UserRole)
        priceId = self.priceListWidget.currentItem().data(Qt.ItemDataRole.UserRole)
        self.load(LenovoAPI.class_wallpaper_list, classId, tagId, priceId, self.sumNum, slot=self.onWallpapersResult)

    def onWallpapersResult(self, result: dict):
        data = result.get('data', {})
        wallpapers = data.get('wallpapers', [])
        for wallpaper in wallpapers:
            wallpaperImg = wallpaper.get('wallpaperImg')
            uri = wallpaperImg[0]['uri']
            imgRemark = wallpaperImg[0].get('imgRemark')
            max_url = wallpaperImg[-1]['uri']
            label = self.addLabel(uri, self.waterfallLayout)

            label.setTitle(imgRemark)
            label.setMaxUrl(max_url)
            label.setMenuData([(wall.get('resolution', ''), wall.get('uri', '')) for wall in wallpaperImg])
        self.lazyTimerStart()

    def clear(self):
        self.waterfallLayout.takeAllWidgets()
        self.lazyWidgets.clear()

    def nextPage(self):
        self.sumNum += 40
        self.loadWallpaperData()

    def onReload(self):
        super().onReload()
        self.sumNum = 0
        self.clear()
        self.loadWallpaperData()
